package es.aa.android.cartomatic.appview;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import es.aa.android.cartomatic.R;
import es.aa.android.cartomatic.appview.adapter.CardAdapter;
import es.aa.android.cartomatic.appview.adapter.DeckAdapter;
import es.aa.android.cartomatic.appview.dialog.CreateDialog;
import es.aa.android.cartomatic.customview.mButtonsHeader;
import es.aa.android.cartomatic.customview.mSimpleHeader;
import es.aa.android.cartomatic.customview.mSimpleList;
import es.aa.android.cartomatic.domain.Card;
import es.aa.android.cartomatic.domain.Deck;
import es.aa.android.cartomatic.implementations.da.SQLiteAccess;
import es.aa.android.cartomatic.implementations.service.NavigationService;
import es.aa.android.cartomatic.interfaces.service.INavigationService;

public class MainView extends FragmentActivity {

    private LinearLayout main;
    private INavigationService navService;
    private SQLiteAccess sqLiteAccess;
    
    private ArrayList<Deck> decksList;
    private ArrayList<Card> cardList;
    private static int deck_mod = 0;
    private static int card_mod = 0;
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_view); 
        init();
    }
    
    private void init() {
    	
    	navService = new NavigationService();
    	sqLiteAccess = new SQLiteAccess();
    	main = (LinearLayout) findViewById(R.id.mainContainer);
    	initHead();
    	initDecks();
    	initCards();
    	
    }
    
    private void initHead() {
    	// Botones
    	Button play = (Button) findViewById(R.id.buttonMainPlay);
    	Button options = (Button) findViewById(R.id.buttonMainOptions);
    	
    	play.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				navService.navigate(MainView.this, BoardView.class);
			}
		});
    	
    	options.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				navService.navigate(MainView.this, OptionsView.class);
			}
		});
    }
    
    private void initDecks() {
    	//Barajas
    	decksList = (ArrayList<Deck>) sqLiteAccess.readDeckLibrary(this);
        final mSimpleList decksView = new mSimpleList(this);
        final mSimpleHeader headerD = new mSimpleHeader(this, "Barajas", null);
        final mButtonsHeader buttonsD = new mButtonsHeader(this);
        
        headerD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                decksView.setExpanded(!decksView.isExpanded());
            }
        });
        buttonsD.setOnShowClickList(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int visibility = decksView.getListVisibility();
				if(visibility == View.GONE){
					if(deck_mod == 1){
						decksList = (ArrayList<Deck>) sqLiteAccess.readDeckLibrary(MainView.this);
						deck_mod = 0;
					}
					DeckAdapter deckAdapter = new DeckAdapter(MainView.this, decksList);
			        decksView.setAdapter(deckAdapter);
			        decksView.setListVisibility(View.VISIBLE);
				}else{
					 decksView.setListVisibility(View.GONE);
				}
		    }
		});
        buttonsD.setOnCreateClickList(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CreateDialog.makeDialog(MainView.this);
				getIntent().putExtra(CreateDialog.ITEM_TO_CREATE, CreateDialog.CREATE_DECK);
				deck_mod = 1;
			}
		});

        decksView.addHeader(headerD);
        decksView.addHeader(buttonsD);
        decksView.setColumns(3, 5);
        
        
        main.addView(decksView);
    }
    
    private void initCards() {
    	//Cartas
    	
    	cardList = (ArrayList<Card>) sqLiteAccess.readCardLibrary(this);
        final mSimpleList cardsView = new mSimpleList(this);
        final mSimpleHeader headerC = new mSimpleHeader(this, "Cartas", null);
        final mButtonsHeader buttonsC = new mButtonsHeader(this);
        
        headerC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                cardsView.setExpanded(!cardsView.isExpanded());
            }
        });
        
        buttonsC.setOnShowClickList(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int visibility = cardsView.getListVisibility();
				if(visibility == View.GONE){
					if(card_mod == 1){
						cardList = (ArrayList<Card>) sqLiteAccess.readCardLibrary(MainView.this);
						card_mod = 0;
					}
					CardAdapter cardAdapter = new CardAdapter(MainView.this, cardList);
			        cardsView.setAdapter(cardAdapter);
					cardsView.setListVisibility(View.VISIBLE);
				}else{
					cardsView.setListVisibility(View.GONE);
				}
				
			}
		});
        
        buttonsC.setOnCreateClickList(new OnClickListener() {
			@Override
			public void onClick(View v) {
				CreateDialog.makeDialog(MainView.this);
				getIntent().putExtra(CreateDialog.ITEM_TO_CREATE, CreateDialog.CREATE_CARD);
				card_mod = 1;
				
			}
			
		});

        cardsView.addHeader(headerC);
        cardsView.addHeader(buttonsC);
        cardsView.setColumns(3, 5); 
        
        main.addView(cardsView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater mi = getMenuInflater();
        //mi.inflate(R.menu.main, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case 0:
                CreateDialog.makeDialog(this);
                break;
        }

        return super.onOptionsItemSelected(item);
    }
    
}
